using Sustainalytics.ESGRatings.BusinessLogic.Computing;

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

namespace Sustainalytics.ESGComputing.Service.Tests.IndexingTests
{
    public class IndexingByHeadsTest : Disposable, IClassFixture<ComparativeGroupFixture>
    {
        private readonly List<Company> _fakeCompanies = new List<Company>();
        private readonly ITestOutputHelper _output;
        private readonly IndexByHeads<Guid, string> _sut;

        public IndexingByHeadsTest(ITestOutputHelper output, ComparativeGroupFixture fixture)
        {
            _fakeCompanies.AddRange(fixture.Companies);
            _sut = IndexByHeads<Guid, string>.Create(_fakeCompanies, x => x.Id, x => x.Region, StringComparer.InvariantCultureIgnoreCase);
            _output = output;
        }

        public class Given_Selector : IndexingByHeadsTest
        {
            public Given_Selector(ITestOutputHelper output, ComparativeGroupFixture fixture)
                : base(output, fixture)
            {
            }

            [Fact]
            public void GetIndex_For_IndustryGroup_Reordering_OneCompanyInGroup()
            {
                //Arrange

                var orderedArray = _fakeCompanies.OrderBy(x => x.Region, StringComparer.InvariantCultureIgnoreCase).ToList();
                var index = 0;

                var expected = new List<int>();

                foreach (var item in orderedArray)
                {
                    if (item.Region == "FRONT")
                    {
                        expected.Add(index);
                    }

                    index++;
                }

                //Act
                var actual = _sut.GetIndex("FRONT");
                //Assert

                _output.WriteLine(actual.Dump());
                Assert.Equal(expected.ToArray(), actual);
            }
        }
    }
}